package com.hsck.order.kds.main.fragment.noodles

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.hsck.order.kds.R
import com.hsck.order.kds.base.isnoam.log.XLog
import com.hsck.order.kds.base.isnoam.view.BaseFragment
import com.hsck.order.kds.base.isnoam.view.SpacesItemDecoration
import com.hsck.order.kds.base.utils.NetworkUtil
import com.hsck.order.kds.databinding.FragmentNoodlesAnVegetablesBinding
import com.hsck.order.kds.main.dialog.SelectNumberDialog
import com.hsck.order.kds.main.dialog.SelectQuantityDialog
import com.hsck.order.kds.main.fragment.noodles.batch.BatchNoodleCuttingActivity
import com.hsck.order.kds.main.fragment.standard.bean.DishManager
import com.hsck.order.kds.main.fragment.standard.bean.TestListBean
import com.hsck.order.kds.main.itf.UpdatableFragmentListener

/**
 * 粉面划菜
 */
class NoodlesAnVegetablesFragment : BaseFragment<FragmentNoodlesAnVegetablesBinding>(),
    UpdatableFragmentListener {
    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
    ): FragmentNoodlesAnVegetablesBinding {
        return FragmentNoodlesAnVegetablesBinding.inflate(inflater)
    }

    private var noodlesAnVegetablesAdapter: NoodlesAnVegetablesAdapter? = null
    private var selectQuantityDialog: SelectQuantityDialog? = null
    private var selectNumberDialog: SelectNumberDialog? = null
    private var itemBean: TestListBean? = null
    private var position: Int? = null


//    override fun loadDataOnce() {
//        super.loadDataOnce()
//        initRecyclerView()
//        noodlesAnVegetablesAdapter?.submitList(DishManager().getDishList())
//        binding.llRoot.visibility = View.VISIBLE
//    }

    override fun onResume() {
        super.onResume()
        initRecyclerView()
        noodlesAnVegetablesAdapter?.submitList(DishManager().getDishList())
        binding.llRoot.visibility = View.VISIBLE
    }

    private fun initRecyclerView() {
        binding.apply {
            if (noodlesAnVegetablesAdapter == null) {
                noodlesAnVegetablesAdapter = activity?.let { NoodlesAnVegetablesAdapter(it) }
            }
            val staggeredGridLayoutManager =
                StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL)
            staggeredGridLayoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE
            recyclerView.layoutManager = staggeredGridLayoutManager
            staggeredGridLayoutManager.isItemPrefetchEnabled = true
            binding.recyclerView.setHasFixedSize(true)
            binding.recyclerView.setItemViewCacheSize(20)
            recyclerView.adapter = noodlesAnVegetablesAdapter
            val spacingInPixels = resources.getDimensionPixelSize(R.dimen.four_column_spacing)
            recyclerView.addItemDecoration(SpacesItemDecoration(spacingInPixels))
            noodlesAnVegetablesAdapter!!.setDishOperationListener(object :
                NoodlesAnVegetablesAdapter.DishOperationListener {
                override fun onBatchOperationAction(itemBean: TestListBean, position: Int) {
                    //批量划菜，进入新界面
//                    val intent = Intent(activity, BatchNoodleCuttingActivity::class.java)
//                    startActivity(intent)
                }

                override fun onDishOperationAction(itemBean: TestListBean, position: Int) {
                    this@NoodlesAnVegetablesFragment.itemBean = itemBean
                    this@NoodlesAnVegetablesFragment.position = position
                    //判断后台返回是否开启快速划菜
                    if (itemBean.openDialog) {
                        when (itemBean.number) {
                            1 -> {
                                //请求划菜接口
                                XLog.i("请求划菜接口")
                            }

                            2 -> {
                                //显示选择1/2弹窗
                                showQuantityDialog()
                            }

                            else -> {
                                //显示设置数量弹窗
                                showNumberDialog()
                            }
                        }
                    } else {
                        //请求划菜接口
                    }
                    XLog.i("数量: ${itemBean.number}, 是否弹窗: ${itemBean.openDialog}")
                }
            })
        }
    }

    /**
     * 数量大于2，设置划菜数量弹窗
     */
    private fun showNumberDialog() {
        if (selectNumberDialog == null) {
            selectNumberDialog = SelectNumberDialog(requireActivity())
        }
        selectNumberDialog!!.show("测试数据", "5")
        selectNumberDialog!!.setSelectNumberListener(object :
            SelectNumberDialog.SelectNumberListener {
            override fun onSelectNumber(i: Int) {
                XLog.i("选择的数量为：$i")
            }
        })
    }

    /**
     * 1、2份数弹窗
     */
    private fun showQuantityDialog() {
        if (selectQuantityDialog == null) {
            selectQuantityDialog = SelectQuantityDialog(requireActivity())
        }
        selectQuantityDialog!!.show("测试数据")
        selectQuantityDialog!!.setSelectQuantityListener(object :
            SelectQuantityDialog.SelectQuantityListener {
            override fun onSelectQuantity(i: Int) {
                XLog.i("选择的数量为：$i")
            }
        })
    }

    override fun onFragmentVisible() {
        if (_binding != null) {
            if (NetworkUtil.isNetworkAvailable(requireActivity())) {
                hideLoadErrorView()
                noodlesAnVegetablesAdapter?.submitList(DishManager().getDishList())
                binding.llRoot.visibility = View.VISIBLE
            } else {
                showLoadErrorView {
                    XLog.i("加载失败，请点击重试")
                }
            }
        }
    }

    override fun onFragmentInvisible() {
        noodlesAnVegetablesAdapter?.stopAllUpdating()
        binding.llRoot.visibility = View.GONE
        //停止接收MQTT
    }

    override fun onDestroyView() {
        super.onDestroyView()
        noodlesAnVegetablesAdapter?.stopAllUpdating()
    }
}